package com.zhou.project.modules.system.user.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.project.configurations.weblogger.annotation.LoggerOperation;
import com.zhou.project.configurations.weblogger.enums.LoggerType;
import com.zhou.project.modules.system.user.entity.SysUser;
import com.zhou.project.modules.system.user.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.Date;

/**
 * @project spring-boot-project
 * @author zhouhonggang
 * @datetime 2021/8/11 13:42
 * @version 1.0.0
 * @description: 用户表现层
 */
@RestController
@RequestMapping("sys/user")
@Api(tags = "[系统管理-用户接口]")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @PostMapping
    @ApiOperation("用户注册")
    @LoggerOperation(module = "user", operationType = LoggerType.INSERT, message = "用户注册")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "name", value = "姓名", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "nick", value = "昵称", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "account", value = "登陆账号", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "password", value = "登陆密码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "email", value = "电子邮箱", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "birthday", value = "出生日期", required = true, dataTypeClass = Date.class)
    })
    public void save(@RequestBody @Valid SysUser entity)
    {
        sysUserService.insert(entity);
    }

    @PutMapping
    @ApiOperation("用户编辑")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主键", required = true, dataTypeClass = Integer.class),
        @ApiImplicitParam(name = "name", value = "姓名", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "nick", value = "昵称", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "account", value = "登陆账号", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "password", value = "登陆密码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "phone", value = "手机号码", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "email", value = "电子邮箱", required = true, dataTypeClass = String.class),
        @ApiImplicitParam(name = "birthday", value = "出生日期", required = true, dataTypeClass = Date.class)
    })
    @LoggerOperation(module = "user", operationType = LoggerType.UPDATE, message = "用户修改")
    public void update(@RequestBody @Valid SysUser entity)
    {
        sysUserService.updateById(entity);
    }

    @DeleteMapping("{id}")
    @ApiOperation("用户逻辑删除")
    @ApiImplicitParam(name = "id", value = "人员主键ID", dataTypeClass = Integer.class)
    @LoggerOperation(module = "user", operationType = LoggerType.DELETE, message = "用户删除")
    public void delete(@PathVariable int id)
    {
        sysUserService.removeById(id);
    }

    @GetMapping("{id}")
    @ApiOperation("用户ID查询")
    @ApiImplicitParam(name = "id", value = "人员主键ID", dataTypeClass = Integer.class)
    @LoggerOperation(module = "user", operationType = LoggerType.SELECT, message = "用户查询")
    public SysUser load(@PathVariable int id)
    {
        return sysUserService.getById(id);
    }

    @PostMapping("page")
    @ApiOperation("用户条件分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "姓名", dataTypeClass = String.class),
            @ApiImplicitParam(name = "nick", value = "昵称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "current", value = "当前页: 默认1", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "size", value = "每页条数: 默认10", dataTypeClass = Integer.class)
    })
    @LoggerOperation(module = "user", operationType = LoggerType.INSERT, message = "用户注册")
    public Page<SysUser> page(@RequestBody @ApiIgnore SysUser entity)
    {
        return sysUserService.page(entity);
    }

}
